import json
import requests
from google.protobuf.json_format import MessageToDict
import base64
from test_pb2 import *
import mitmproxy.http
from bs4 import BeautifulSoup
import urllib.parse
import emoji
import configparser




def get_html(data, cont, leixin):
    html4 = requests.post("https://www.douyin.com/user/" + data)
    soup = BeautifulSoup(html4.text, 'lxml')
    shuju = soup.find("script", type="application/json").text
    shuju1 = json.loads(urllib.parse.unquote(shuju),strict=False)
    print(shuju1)
    file = open("userlog.txt", 'a',encoding='utf8')
    text = str(shuju1)
    file.write(text + '\n')
    file.close()
    
    mingzi = shuju1['51']['user']['user']['nickname']
    uid1 = shuju1['51']['user']['user']['secUid']
    zongfensi = shuju1['51']['user']['user']['mplatformFollowersCount']
    share = shuju1['51']['user']['user']['shareQrcodeUrl']
    guanzhu = shuju1['51']['user']['user']['followingCount']
    userid = shuju1['51']['user']['user']['uniqueId']
    if leixin == 1:
        file = open("ax.txt", 'a',encoding='utf8')
        text = str(str(emoji.demojize(mingzi)) + "====" + str(uid1) + "====" + str(zongfensi) + "====" + str(share) + "====" + str(guanzhu) + "====" + str(userid) + "====" + str(cont))
        file.write(text + '\n')
        file.close()
    elif leixin == 2:
        file = open("ax.txt", 'a',encoding='utf8')
        text = str(str(emoji.demojize(mingzi)) + "====" + str(uid1) + "====" + str(zongfensi) + "====" + str(share) + "====" + str(guanzhu) + "====" + str(userid) + "====" + str(cont))
        file.write(text + '\n')
        file.close()
    elif leixin == 3:
        file = open("ax.txt", 'a',encoding='utf8')
        text = str(str(emoji.demojize(mingzi)) + "====" + str(uid1) + "====" + str(zongfensi) + "====" + str(share) + "====" + str(guanzhu) + "====" + str(userid) + "====" + str(cont))
        file.write(text + '\n')
        file.close()
    elif leixin == 4:
        file = open("ax.txt", 'a',encoding='utf8')
        text = str(str(emoji.demojize(mingzi)) + "====" + str(uid1) + "====" + str(zongfensi) + "====" + str(share) + "====" + str(guanzhu) + "====" + str(userid) + "====" + str(emoji.demojize(cont)))
        file.write(text + '\n')
        file.close()


def on_message(data):
    cf = configparser.ConfigParser()
    cf.read("config.ini")
    danmu_resp = test()
    danmu_resp.ParseFromString(data)
    message = MessageToDict(danmu_resp, preserving_proto_field_name=True)
    for message in message['message']:
        file = open("log.txt", 'a',encoding='utf8')
        text = str(message)
        file.write(text + '\n')
        file.close()
        
        method = message["method"]
        payload = bytes(base64.b64decode(message["payload"].encode()))
        if method == "WebcastMemberMessage":
            member_message = WebcastMemberMessage()
            member_message.ParseFromString(payload)
            obj1 = MessageToDict(member_message, preserving_proto_field_name=True)
            val = cf.getint("config", "room")
            if val == 1:
                print("欢迎：" + obj1['user'][0]['nickname'] + "加入直播间"+"---------")
                get_html(obj1['user'][0]['secUid'], "加入直播间", 1)
            elif val == 0:
                print("欢迎：" + obj1['user'][0]['nickname'] + "加入直播间" )

        elif method == "WebcastGiftMessage":
            member_message3 = WebcastGiftMessage()
            member_message3.ParseFromString(payload)
            obj3 = MessageToDict(member_message3, preserving_proto_field_name=True)
            jiexi2 = obj3
            print(jiexi2['common'][0]['describe'])


            val = cf.getint("config", "gif")
            if val == 1:
                get_html(jiexi2['user'][0]['secUid'], "礼物消息", 2)
                print(jiexi2['user'][0]['nickname'])
            elif val==0:
                print(jiexi2['user'][0]['nickname'])

        elif method == "WebcastLikeMessage":
            member_message4 = WebcastLikeMessage()
            member_message4.ParseFromString(payload)
            obj4 = MessageToDict(member_message4, preserving_proto_field_name=True)
            jiexi = obj4


            val = cf.getint("config", "like")
            if val == 1:
                get_html(jiexi['user'][0]['secUid'], "点赞消息", 3)
                print(jiexi['user'][0]['nickname'] + "点赞了主播")
            elif val==0:
                print(jiexi['user'][0]['nickname'] + "点赞了主播")
        elif method == "WebcastChatMessage":
            member_message5 = WebcastChatMessage()
            member_message5.ParseFromString(payload)
            obj5 = MessageToDict(member_message5, preserving_proto_field_name=True)
            jiexi1 = obj5


            val = cf.getint("config", "chat")
            if val == 1:
                get_html(jiexi1['user'][0]['secUid'], jiexi1['content'], 4)
                print(jiexi1['user'][0]['nickname'] + ":" + jiexi1['content'] )
            elif val ==0 :
                print(jiexi1['user'][0]['nickname'] + ":" + jiexi1['content'] )

class Counter:

    def response(self, flow: mitmproxy.http.HTTPFlow):
        if flow.request.host == "live.douyin.com":
            data = flow.response.content
            on_message(data)


addons = [
    Counter()
]
